/**
 * 品牌
 * 文件命名格式：zmn.模块名称.表名称.功能.jas
 * @author duanzucai
 * @since 2018/11/10
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate','jquery'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;
    var laydate = layui.laydate;
    zmnLayUtil.layFormVerify(form);
    let faultGroupVOList =json.faultGroupVOList;
    if(faultGroupVOList&&faultGroupVOList.length>0){
        faultGroupVOList.forEach(e=>{
            table.init('zmn-table'+e.groupId, {
                page: false,
                limit: 100000,
                done: function (res) {
                    res.data.forEach(function (item,index) {
                        if(item.general=='默认'){
                            layui.$('#zmn-table'+e.groupId).next('div').find(".layui-table-body tbody tr[data-index='"+index+"'] td[data-field='general']").css({'color': '#ff0000' });
                        }
                        if(item.status.indexOf('停用')>0){
                            layui.$('#zmn-table'+e.groupId).next('div').find(".layui-table-body tbody tr[data-index='"+index+"'] td[data-field='status']").css({'color': '#DCDCDC' });
                        }
                    });
                }
            });
            table.on('edit(zmn-table'+e.groupId+')', function (obj) {
                if (obj.field === 'sort') {
                    if(obj.value<1||obj.value>1000000){
                        return layer.alert("排序值必须大于1小于1000000！");
                    }
                    zmnPage.laySort(obj, "/service/item/updateSort.action", obj.data.itemId);
                }
            });
        });
    }

    $(".operator").click(function () {
        let params = $(this).attr("param").split(",");
        let groupId = params[0];
        let itemId = params[1];
        let status = params[2];
        let itemName = params[3];
        let priceSplit = params[4];
        $("#deleteBtn").addClass('layui-bg-gray');
        $("#feeDetailBtn").addClass('layui-bg-gray');
        if(status==1){
            $("#deleteBtn").removeClass('layui-bg-gray');
        }
        if(priceSplit==2){
            $("#feeDetailBtn").removeClass('layui-bg-gray');
        }
        let html = $("#operatorDiv").html();
        let newDom = html.replace(/_itemId/g,itemId).replace(/_groupId/g,groupId).replace(/_status/g,status).replace(/_itemName/g,itemName).replace(/_priceSplit/g,priceSplit);

        layer.tips(newDom, this, {
            shade: [0],
            shadeClose: true,
            area: ['300px', '128px'],
            time: 0, //20秒后自动关闭
            tips: [4, '#FFFFFF'],
        });
    });
    exports('serv_service_group_list', {});
});

function batchAddWarrantyInfo(tariffId, servCategId, categOneId) {
    let tables = layui.$('.table-filter');
    var ids = new Array();
    tables.each(function (i, item) {
        let id = tables[i].id;
        let currentIds = layui.table.checkStatus(id).data.map((item) => item.itemId);
        ids = ids.concat(currentIds);
    });

    if (ids.length <= 0) {
        layer.msg("请先勾选项目！");
        return false;
    }
    let itemIds = ids.join(",");
    var url = "/service/item/batchWarrantyInfoDetail.action?tariffId="+tariffId+"&servCategId="+servCategId+"&categOneId="+categOneId+"&itemIds="+itemIds;
    zmnLayUtil.openTabsPageCustom(parent, url, "快速设置保修信息");
}

//新增故障分组
function toAddGroup(servCategId,categOneId,tariffId) {
    layer.open({
        type: 2,
        title:'新增分组',
        area: ['650px', '420px'],
        content: '/service/group/detail.action?servCategId='+servCategId+"&categOneId="+categOneId+"&tariffId="+tariffId
    });
}

//复制分组（包含分组下的服务项）
function toCopyGroup(tariffId,servCategId,categOneId) {
    layer.open({
        type: 2,
        title:'复制分组',
        area: ['650px', '460px'],
        content: '/service/group/copyGroup.action?tariffId='+tariffId+"&servCategId="+servCategId+"&categOneId="+categOneId
    });
}

//修改故障分组
function toEditGroup(groupId) {
    layer.open({
        type: 2,
        title:'修改分组',
        area: ['650px', '420px'],
        content: '/service/group/detail.action?groupId='+groupId
    });
}

//删除分组
function toDeleteGroup(groupId) {
    layer.confirm('确定要删除该分组吗？', {
        btn: ['确定', '取消']
    }, function () {
        zmnPage.ajaxSimple("/service/group/deleteGroup.action", {groupId: groupId}, 1000);
    });
}



function deleteItem(itemId, name,status){
    if(status!=1){
        return layer.msg('请先停用，后删除');
    }
    layer.confirm('确定要删【'+ name + '】服务项吗？', {
        btn: ['确定', '取消']
    }, function () {
        zmnPage.ajaxSimple("/service/item/deleteItem.action", {itemId: itemId}, 1000);
    });
}

function viewFeeDetail(itemId,groupId,priceSplit) {
    if(priceSplit!=2){
        return layer.msg('一口价区分明细才有费用明细');
    }
    let _$= layui.$;
    let currentTr = _$("#zmn-table"+groupId+" tbody tr").find("td:eq(2):contains("+itemId+")").parent('tr');
    let showTds = currentTr.find('td.show');
    let type=showTds[0].innerText;
    if(type=='明细一'){
        _$("#showTipsDiv .type1").show();
        _$("#showTipsDiv .type2").hide();
    }else{
        _$("#showTipsDiv .type1").hide();
        _$("#showTipsDiv .type2").show();
    }
    let showHtml = _$("#showTipsDiv").html();
    showTds.each(function (i,item) {
        let current = _$(item);
        let key = current.attr("class").replace("show ","");
        showHtml = showHtml.replace("_"+key,current.text())
    });
    layer.open({
        title:'费用明细',
        area: ['400px', '260px'],
        content: showHtml,
        btn:null
    });

}


function reload(msg){
    parent.layui.layer.msg(msg);
    window.location.reload();
}
